package cn.xclink.kernel.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.xclink.common.mapper.BaseMapper;
import cn.xclink.common.service.BaseService;
import cn.xclink.kernel.entity.User;
import cn.xclink.kernel.entity.UserOrg;
import cn.xclink.kernel.entity.UserResource;
import cn.xclink.kernel.entity.UserRole;
import cn.xclink.kernel.mapper.UserMapper;

@Service
public class UserService extends BaseService<User> {

	@Autowired
	private UserMapper mapper;
	
	@Autowired
	private UserOrgService userOrgService;

	@Autowired
	private UserRoleService userRoleService;
	
	@Autowired
	private UserResourceService userResourceService;

	@Autowired
	private PasswordHelper passwordHelper;
	
	
	@Override
	public BaseMapper<User> getMapper() {
		return mapper;
	}
	
	@Override
	public List<User> find(User user) {
		return mapper.selectUser(user);
	}
	
	/**
	 * 保存用户
	 * @param user
	 */
	public int saveUser(User user) throws Exception {
		passwordHelper.encryptPassword(user);
		int result = super.save(user);
		
		UserRole duserRole = new UserRole();
		duserRole.setUserId(user.getUserId());
		userRoleService.deleteByProperties(duserRole);
		List<Long> roleIds = Arrays.asList(user.getRoleIds());
		for(Long roleId : roleIds){
			UserRole userRole = new UserRole();
			userRole.setUserId(user.getUserId());
			userRole.setRoleId(roleId);
			userRole.setAvailable("Y");
			userRoleService.save(userRole);
		}
		
		UserOrg duserOrg = new UserOrg();
		duserOrg.setUserId(user.getUserId());
		userOrgService.deleteByProperties(duserOrg);
		List<Long> orgIds = Arrays.asList(user.getOrgIds());
		for(Long orgId : orgIds){
			UserOrg userOrg = new UserOrg();
			userOrg.setUserId(user.getUserId());
			userOrg.setOrgId(orgId);
			userOrg.setAvailable("Y");
			userOrgService.save(userOrg);
		}
		return result;
	}
	
	/**
	 * 编辑用户
	 */
	public int updateUser(User user) throws Exception {
		
		int result = super.updateNotNull(user);
		UserRole duserRole = new UserRole();
		duserRole.setUserId(user.getUserId());
		userRoleService.deleteByProperties(duserRole);
		List<Long> roleIds = Arrays.asList(user.getRoleIds());
		for(Long roleId : roleIds){
			UserRole userRole = new UserRole();
			userRole.setUserId(user.getUserId());
			userRole.setRoleId(roleId);
			userRole.setAvailable("Y");
			userRoleService.save(userRole);
		}
		
		UserOrg duserOrg = new UserOrg();
		duserOrg.setUserId(user.getUserId());
		userOrgService.deleteByProperties(duserOrg);
		List<Long> orgIds = Arrays.asList(user.getOrgIds());
		for(Long orgId : orgIds){
			UserOrg userOrg = new UserOrg();
			userOrg.setUserId(user.getUserId());
			userOrg.setOrgId(orgId);
			userOrg.setAvailable("Y");
			userOrgService.save(userOrg);
		}
		return result;
	}

	/**
	 * 删除用户
	 */
	@Override
	public int delete(Object key) throws Exception {
		Long userId = Long.parseLong(key+"");
		//1.删除分配给用户的授权部门
		UserOrg userOrg = new UserOrg();
		userOrg.setUserId(userId);
		userOrgService.deleteByProperties(userOrg);
		
		//2.删除分配给用户的角色
		UserRole userRole = new UserRole();
		userRole.setUserId(userId);
		userRoleService.deleteByProperties(userRole);
		
		//3.删除分配给用户的资源
		UserResource userResource = new UserResource();
		userResource.setUserId(userId);
		userResourceService.deleteByProperties(userResource);
		return super.delete(userId);
	}

	/**
	 * 修改密码
	 * @param userId
	 * @param newPassword
	 */
	public void changePassword(Long userId, String newPassword) throws Exception {
		User user = this.get(userId);
		user.setPassword(newPassword);
		passwordHelper.encryptPassword(user);
		super.updateNotNull(user);
	}
	
	/**
	 * 根据用户帐号查找用户
	 * @param account
	 * @return
	 */
	public User findUserByAccount(String account) {
		User user = new User();
		user.setAccount(account);
		return this.findOne(user);
	}
}
